package com.sthz.facepay.service.impl;

import com.sthz.facepay.enums.ApiResultEnum;
import com.sthz.facepay.mapper.AgentMapper;
import com.sthz.facepay.model.Agent;
import com.sthz.facepay.model.AgentRes;
import com.sthz.facepay.service.AgentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author luli
 * @version jdk 1.8
 * @date 2019/7/16 14:40
 */
@Service
public class AgentServiceImpl implements AgentService {

    @Autowired
    AgentMapper agentMapper;

    /**
     * 新增代理商
     * @param agent
     * @return
     */
    @Override
    @Transactional
    public int save(Agent agent) {
        boolean flag = agentMapper.insert(agent);
        if (flag) {
            return ApiResultEnum.SUCCESS.getErrCode();
        }
        return ApiResultEnum.FAIL.getErrCode();
    }

    /**
     * 根据ID修改代理商信息
     * @param agent
     * @return
     */
    @Override
    @Transactional
    public int modify(Agent agent) {
        Agent ag = agentMapper.getById(agent.getId());
        if(ag != null){
            boolean flag = agentMapper.updateByPrimaryKey(agent);
            if (flag) {
                return ApiResultEnum.SUCCESS.getErrCode();
            }
            return ApiResultEnum.FAIL.getErrCode();
        }
        return ApiResultEnum.FAIL.getErrCode();
    }

    /**
     * 查询代理商列表
     * @return
     */
    @Override
    public List<Agent> findAll() {
        return agentMapper.findAll();
    }

    /**
     * 据代理商名称模糊查询
     * @param name
     * @return
     */
    @Override
    public List<Agent> selectAgentByName(String name) {
        return agentMapper.selectAgentByName(name);
    }

    /**
     * 总代理商数
     * @return
     */
    @Override
    public AgentRes agentStatistics() {
        AgentRes agentRes = new AgentRes();
        // 总代理商数
        agentRes.setGeneralAgentNum(agentMapper.getAgentTotal());
        // 本周新增代理商数
        agentRes.setWeekNewAddAgentNum(agentMapper.getWeekNewAddAgentTotal());
        // 正在使用代理商数
        agentRes.setInUseAgentNum(agentMapper.getInUseAgentTotal());
        // 过期代理商数
        agentRes.setExpireAgentNum(agentMapper.getExpireAgentTotal());

        return agentRes;
    }
}
